package com.yeyaomai.dksns.flowis.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import com.yeyaomai.dksns.flowis.dao.DetailDao;
import com.yeyaomai.dksns.flowis.dao.MongoDBBasicDao;
import com.yeyaomai.dksns.flowis.dao.TrendAnalysisHourDao;
import com.yeyaomai.dksns.flowis.domain.TrendAnalysisHour;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisStatDetailDTO;
import com.yeyaomai.dksns.flowis.service.TrendAnalysisHourService;
import com.yeyaomai.dksns.flowis.util.CollectionNames;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

/**
 * 时段分析按时service层实现
 * 
 * @author xiaqin date:2012-05-09
 */
@Service
public class TrendAnalysisHourServiceImpl extends
		MongoDBBasicDao<TrendAnalysisHour> implements TrendAnalysisHourService {

	@Resource
	private TrendAnalysisHourDao trendAnalysisHourDao;

	@Resource
	private DetailDao detailDao;

	@Override
	public void addTrendAnalysisDay(TrendAnalysisHour trendAnalysisHour) {
		trendAnalysisHourDao.addTrendAnalysisDay(trendAnalysisHour);

	}

	@Override
	public TrendAnalysisHour getTrendAnalysisHourById(long id) {

		return trendAnalysisHourDao.getTrendAnalysisHourById(id);
	}

	@Override
	public List<TrendAnalysisHour> getTrendAnalysisHourByHour(
			Date fromTrendDay, Date toTrendDay) {

		return trendAnalysisHourDao.getTrendAnalysisHourByHour(fromTrendDay,
				toTrendDay);
	}

	@Override
	public TrendAnalysisHour getCurrentData(Date date) {
		String fromTrendDay = VelocityToolDateUtils.formathour.format(date)
				+ ":00";
		String toTrendDay = VelocityToolDateUtils.format.format(date.getTime());
		TrendAnalysisHour trendhour = new TrendAnalysisHour();
		int pageview = this.getPageViewByTime(fromTrendDay, toTrendDay);
		int uv = this.getUvByTime(fromTrendDay, toTrendDay);
		int ip = this.getIpByTime(fromTrendDay, toTrendDay);
		int newuv = this.getNewUvByTime(fromTrendDay, toTrendDay);
		SimpleDateFormat formats = new SimpleDateFormat("yyyyMMdd");
		String trendDay = formats.format(date.getTime());
		SimpleDateFormat fmat = new SimpleDateFormat("HH");
		String hour = fmat.format(date.getTime()) + ":00";
		try {
			trendhour.setTrendDay(formats.parse(trendDay));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		trendhour.setHour(hour);
		trendhour.setPageView(pageview);
		trendhour.setUniqueHost(uv);
		trendhour.setIp(ip);
		trendhour.setNewUniqueHost(newuv);
		trendhour.setAverage(String.format("%.2f", (double) pageview / uv));
		return trendhour;
	}

	public long countTrendAnalysisDayByTime(String fromTrendDay,
			String toTrendDay) {
		Query query = new Query(Criteria.where("access_date").gte(fromTrendDay)
				.lt(toTrendDay));

		long pvcount = mongoTemplate.count(query, FlowisStatDetailDTO.class);
		return pvcount;
	}

	/**
	 * 动态处理PV
	 */
	private int getPageViewByTime(String fromTrendDay, String toTrendDay) {

		int pvcount = (int) this.countTrendAnalysisDayByTime(fromTrendDay,
				toTrendDay);
		return pvcount;
	}

	/**
	 * 动态处理UV
	 */
	private int getUvByTime(String fromTrendDay, String toTrendDay) {
		String mapFunction = "{ cookieId: 0}";
		String reduceFunction = "function(doc, prev) {prev.cookieId+=1}";
		Criteria criteria = Criteria.where("access_date").gte(fromTrendDay)
				.lt(toTrendDay);
		int uvcount = detailDao.group(criteria, CollectionNames.COOKIEID,
				mapFunction, reduceFunction, FlowisStatDetailDTO.class)
				.getKeys();
		return uvcount;
	}

	/**
	 * 动态处理IP
	 */
	private int getIpByTime(String fromTrendDay, String toTrendDay) {
		String mapFunction = "{ip: 0}";
		String reduceFunction = "function(doc, prev) {prev.ip+=1}";
		Criteria criteria = Criteria.where("access_date").gte(fromTrendDay)
				.lt(toTrendDay);
		int ipcount = detailDao.group(criteria, CollectionNames.IP,
				mapFunction, reduceFunction, FlowisStatDetailDTO.class)
				.getKeys();
		return ipcount;
	}

	/**
	 * 动态处理newuv
	 */
	private int getNewUvByTime(String fromTrendDay, String toTrendDay) {
		int newuvcount = 0;
		String mapFunction = "{cookieId: 0}";
		String reduceFunction = "function(doc, prev) {prev.cookieId+=1}";
		Criteria criteria = Criteria.where("access_date").gte(fromTrendDay)
				.lt(toTrendDay).and("visitCount").is(1);
		newuvcount = detailDao.group(criteria, CollectionNames.COOKIEID,
				mapFunction, reduceFunction, FlowisStatDetailDTO.class)
				.getKeys();
		/*
		 * for (FlowisStatDetailDTO engine : result) { if
		 * (engine.getVisitCount().equals(1)) { newuvcount =
		 * Integer.valueOf(engine.getCookieId().split("\\.")[0]); } }
		 */
		return newuvcount;
	}

}
